/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.gitee.minimalismstyle.fresh.sys.service.impl

import com.alibaba.fastjson.JSON
import com.gitee.minimalismstyle.fresh.common.core.const.CacheConst
import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.core.extra.jsonCLeanUp
import com.gitee.minimalismstyle.fresh.common.data.beetl.service.impl.LongModelBaseServiceImpl
import com.gitee.minimalismstyle.fresh.sys.dao.SysRouteDao
import com.gitee.minimalismstyle.fresh.sys.api.model.SysRoute
import com.gitee.minimalismstyle.fresh.sys.api.service.SysRouteService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Primary
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.stereotype.Service

/**
 * 网关路由业务实现
 * @author maochao
*/
@Primary
@Service
class SysRouteServiceImpl: SysRouteService, LongModelBaseServiceImpl<SysRouteDao, SysRoute>(){
    @Autowired
    private lateinit var redisTemplate: StringRedisTemplate

    override fun findAllByEnabled(): R<List<SysRoute>> {
        return R.success(dao.findAllByEnabled())
    }

    override fun reload() : R<Boolean> {
        var routes = dao.findAllByEnabled()
        var routeJson = JSON.toJSON(routes).toString().jsonCLeanUp()
        redisTemplate.opsForValue().set(CacheConst.ROUTES_KEY, routeJson)
        redisTemplate.convertAndSend(CacheConst.ROUTES_RELOAD_TOPIC, "网关缓存更新")
        return R.success(true)
    }
}